﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IQKS.AWP.BusinessAccessLayer;
using IQKS.AWP.BusinessObject;

public partial class Admin_UserMaster : System.Web.UI.Page
{
    #region events
    Label lblErrorMessage;
    protected void Page_Load(object sender, EventArgs e)
    {
        lblErrorMessage = (Label)Master.FindControl("lblErrorMessage");
        if (!IsPostBack)
        {
            GetData(null,null);
            GetRole();
            //Session["UserID"] = Guid.NewGuid();
        }
    }

    private void GetData(string email,string rolename)
    {
        gvSearchResult.DataSource = GetDataSet(email, rolename);
        gvSearchResult.DataBind();

        //if (hdnInput.Value == "search")
        //{
        //    DataSet objDs = new DataSet();
        //    objDs = GetDataSet();
        //    DataView objDv = new DataView();
        //    objDv = objDs.Tables[0].DefaultView;
        //    objDv.RowFilter = "LanguageName  LIKE '*" + txtUserId.Text.Trim() + "*'";
        //    gvSearchResult.DataSource = objDv;
        //}
        //else
        //{
        //    gvSearchResult.DataSource = GetDataSet();
        //}
        //gvSearchResult.DataBind();
    }

    
    protected void gvSearchResult_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //throw new NotImplementedException();
        gvSearchResult.PageIndex = e.NewPageIndex;
        gvSearchResult.DataSource = GetDataSet(txtUserId.Text.Trim(),ddlRoles.SelectedValue.ToString());
        gvSearchResult.DataBind();
    }


    private void GetRole()
    {
        ddlRoles.DataSource = GetRoleDataSet();
        ddlRoles.DataTextField = "RoleTitle";
        ddlRoles.DataValueField = "RoleName";
        ddlRoles.DataBind();
        CommonUtility.AddSelectToDropDownAsEmptyValue(ref ddlRoles);
    }

    private DataSet GetRoleDataSet()
    {

        DataSet objDs = new DataSet();
        try
        {
            objDs = new RoleMasterBAL().GetAllRoleDetails();
        }
        catch (Exception exception)
        {
            exception.AddErrorToLog(); //throw new ApplicationException(exception.Message);
        }
        return objDs;

    }

    protected void gvSearchResult_OnRowEditing(object sender, GridViewEditEventArgs e)
    {
        //throw new NotImplementedException();
    }

    protected void gvSearchResult_OnRowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //throw new NotImplementedException();
    }

    protected void BtnSearch_OnClick(object sender, EventArgs e)
    {
        //throw new NotImplementedException();
        GetData(txtUserId.Text.Trim(), ddlRoles.SelectedValue.ToString());
    }

    #endregion

    #region Methods
    private DataSet GetDataSet(string email, string rolename)
    {
        DataSet objDs=null;
        try
        {
            //objDs = new LanugageMasterBAL().GetLanguageMaster();
            objDs = new UserMasterBAL().GetUserMaster(email, rolename);
        }
        catch (Exception exception)
        {
            exception.AddErrorToLog(); //throw new ApplicationException(exception.Message);
        }
        return objDs;


        //DataSet dsReport = new DataSet();
        //DataTable dt = new DataTable();
        //dt.Columns.Add("IsArchive", Type.GetType("System.Boolean"));
        //dt.Columns.Add("User_ID");
        //dt.Columns.Add("User_Name");
        //dt.Columns.Add("Role_Name");
        //dt.Columns.Add("Full_Name");
        //dt.Columns.Add("EmailID");

        //DataRow r = dt.NewRow();

        //r["User_ID"] = "1";
        //r["User_Name"] = "ShripatK";
        //r["Role_Name"] = "Assessor-Research Lead";
        //r["Full_Name"] = "Shripat Kakarla";
        //r["EmailID"] = "shripat.K@unilever.com";
        //r["IsArchive"] = 1;
        //dt.Rows.Add(r);

        //DataRow r1 = dt.NewRow();

        //r1["User_ID"] = "2";
        //r1["User_Name"] = "RemoD";
        //r1["Role_Name"] = "Candidate";
        //r1["Full_Name"] = "Remo d'souza";
        //r1["EmailID"] = "remo.d@unilever.com";
        //r1["IsArchive"] = 1;
        //dt.Rows.Add(r1);

        //DataRow r2 = dt.NewRow();

        //r2["User_ID"] = "3";
        //r2["User_Name"] = "MiteshiD";
        //r2["Role_Name"] = "IQ-KS Admin";
        //r2["Full_Name"] = "Mitesh Deshmukh";
        //r2["EmailID"] = "mitesh.d@unilever.com";
        //r2["IsArchive"] = 1;
        //dt.Rows.Add(r2);

        
       

        //DataRow r3 = dt.NewRow();

        //r3["User_ID"] = "4";
        //r3["User_Name"] = "MiteshiD";
        //r3["Role_Name"] = "Candidate";
        //r3["Full_Name"] = "Britney Cooper";
        //r3["EmailID"] = "britney.Cooper@gmail.com";
        //r3["IsArchive"] = 1;
        //dt.Rows.Add(r3);

        //DataRow r4 = dt.NewRow();

        //r4["User_ID"] = "4";
        //r4["User_Name"] = "MiteshiD";
        //r4["Role_Name"] = "Candidate";
        //r4["Full_Name"] = "HK Matthews";
        //r4["EmailID"] = "HK.Matthews@gmail.com";
        //r4["IsArchive"] = 1;
        //dt.Rows.Add(r4);

        //DataRow r5 = dt.NewRow();

        //r5["User_ID"] = "5";
        //r5["User_Name"] = "MiteshiD";
        //r5["Role_Name"] = "Candidate";
        //r5["Full_Name"] = "Ja Smit";
        //r5["EmailID"] = "Ja.Smit@gmail.com";
        //r5["IsArchive"] = 1;
        //dt.Rows.Add(r5);


        //dsReport.Tables.Add(dt);
        ////dsReport = DataUtils.ExecuteDataset("usp_GetBranchDetails", txtSearch.Text.Trim(), txtbranchCode.Text.Trim());
        //return dsReport;
    }

    protected void BtnClear_OnClick(object sender, EventArgs e)
    {
        //throw new NotImplementedException();
//        txtUserName.Text = "";
        txtUserId.Text = "";
        GetData(null,null);
    }

    protected void btnAddNew_OnClick(object sender, EventArgs e)
    {
        //throw new NotImplementedException();
        //Session["UserID"] = "0";
        //Response.Redirect("~/Admin/UserMasterModify.aspx");

        Response.Redirect("~/Admin/UserMasterModify.aspx?userid=0&mod=in");
    }

    protected void gvSearchResult_OnRowCommand(object sender, GridViewCommandEventArgs e)
    {      
        if (e.CommandName == "Reset")
        {
            Guid userid = new Guid(e.CommandArgument.ToString());
            UserMasterBAL objUMBAL = new UserMasterBAL();
            string message = objUMBAL.SetDefaultPassword(userid);
        }    
    }

    protected void gvSearchResult_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton l = (LinkButton)e.Row.FindControl("lnkDelete");
            if (Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "isapproved")) == 0)
            {
                l.Attributes.Add("onclick", "javascript:return " + "confirm('Are you sure you want to active " + DataBinder.Eval(e.Row.DataItem, "fullname") + " User?')");
            }
            else
            {
                l.Attributes.Add("onclick", "javascript:return " + "confirm('Are you sure you want to delete " + DataBinder.Eval(e.Row.DataItem, "fullname") + " User?')");
            }

            LinkButton lr = (LinkButton)e.Row.FindControl("lnkReset");
            if (Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "isapproved")) == 0)
            {
                lr.Attributes.Add("onclick", "javascript:return " + "confirm('Are you sure you want to reset " + DataBinder.Eval(e.Row.DataItem, "fullname") + " User?')");
            }
            else
            {
                lr.Attributes.Add("onclick", "javascript:return " + "confirm('Are you sure you want to reset " + DataBinder.Eval(e.Row.DataItem, "fullname") + " User?')");
            }
        }
    }

    protected void gvSearchResult_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        UserMasterBAL objUMBAL=new UserMasterBAL();
        //int intLanguageID = (int)gvSearchResult.DataKeys[e.RowIndex].Value;
        string strStatus = Convert.ToString(gvSearchResult.Rows[e.RowIndex].Cells[4].Text).ToUpper() == "ACTIVE" ? "DELETE" : "ACTIVE";
        string email = e.Values[2].ToString();
        string result = objUMBAL.ChangeUserStatus(CommonUtility.GetLoggedOnUserId(), email, strStatus);
        GetData(txtUserId.Text.Trim(), ddlRoles.SelectedValue.ToString());
    }
    #endregion
   
}